package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.MainExListBean;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/4/25.
 */

public class MainExListAdapter extends BaseExpandableListAdapter{
    private Context context;
    private List<MainExListBean> data = new ArrayList<>();

    public MainExListAdapter(Context context){
        this.context = context;
    }

    public void setData(List<MainExListBean> data){
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).getStringList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getStringList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
        GroupVH holder = null;
        if (view ==null){
            holder = new GroupVH();
            view = LayoutInflater.from(context).inflate(R.layout.layout_ex_main_group_view, null);

            holder.group_image = view.findViewById(R.id.group_image);
            holder.group_title = view.findViewById(R.id.group_title);
            view.setTag(holder);
            AutoUtils.auto(view);
        }else {
            holder = (GroupVH) view.getTag();
        }

        holder.group_title.setText(data.get(groupPosition).getTitle());
        if (isExpanded){
            ImageManager.getInstance().loadImage(context, holder.group_image, R.drawable.icon_room_check, ImageManager.ImageType.NARMOL);
        }else {
            ImageManager.getInstance().loadImage(context, holder.group_image, R.drawable.icon_link_close, ImageManager.ImageType.NARMOL);
        }

        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        ChildVH holder = null;
        if (view ==null){
            holder = new ChildVH();
            view = LayoutInflater.from(context).inflate(R.layout.layout_ex_main_child_view, null);

            holder.child_head = view.findViewById(R.id.child_head);
            holder.child_follow = view.findViewById(R.id.child_follow);
            holder.child_title = view.findViewById(R.id.child_title);
            view.setTag(holder);
            AutoUtils.auto(view);
        }else {
            holder = (ChildVH) view.getTag();
        }

//        holder.child_title.setText(data.get(groupPosition).getStringList().get(childPosition));
//        if (isExpanded){
//            ImageManager.getInstance().loadImage(context, holder.group_image, R.drawable.icon_room_check, ImageManager.ImageType.NARMOL);
//        }else {
//            ImageManager.getInstance().loadImage(context, holder.group_image, R.drawable.icon_link_close, ImageManager.ImageType.NARMOL);
//        }

        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class GroupVH{
        private TextView group_title;
        private ImageView group_image;
    }

    private class ChildVH{
        private ImageView child_head, child_follow;
        private TextView child_title;
    }
}
